package com.wofish.web.controller.goods;

import com.wofish.common.annotation.Log;
import com.wofish.common.core.controller.BaseController;
import com.wofish.common.core.domain.AjaxResult;
import com.wofish.common.core.page.TableDataInfo;
import com.wofish.common.enums.BusinessType;
import com.wofish.common.json.JsonUtil;
import com.wofish.common.utils.poi.ExcelUtil;
import com.wofish.goods.domain.Goods;
import com.wofish.goods.domain.GoodsType;
import com.wofish.goods.domain.GoodsTypeProps;
import com.wofish.goods.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品类型 信息操作处理
 * 
 * @author wofish
 * @date 2019-04-14
 */
@Controller
@RequestMapping("/goods/goodsType")
public class GoodsTypeController extends BaseController
{
    private String prefix = "goods/goodsType";
	
	@Autowired
	private IGoodsTypeService goodsTypeService;

	@Autowired
	private IGoodsTypePropsService goodsTypePropsService;


	@Autowired
	private IGoodsTypeSpecService goodsTypeSpecService;

	@Autowired
	private ITypeBrandService typeBrandService;

	@Autowired
	private IGoodsService goodsService;

	@RequiresPermissions("goods:goodsType:view")
	@GetMapping()
	public String goodsType()
	{
	    return prefix + "/goods_type";
	}
	
	/**
	 * 查询商品类型列表
	 */
	@RequiresPermissions("goods:goodsType:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(GoodsType goodsType)
	{
		startPage();
        List<GoodsType> list = goodsTypeService.selectGoodsTypeList(goodsType);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商品类型列表
	 */
	@RequiresPermissions("goods:goodsType:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsType goodsType)
    {
    	List<GoodsType> list = goodsTypeService.selectGoodsTypeList(goodsType);
        ExcelUtil<GoodsType> util = new ExcelUtil<GoodsType>(GoodsType.class);
        return util.exportExcel(list, "goodsType");
    }
	
	/**
	 * 新增商品类型
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商品类型
	 */
	@RequiresPermissions("goods:goodsType:add")
	@Log(title = "商品类型", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
    public AjaxResult addSave(GoodsType goodsType, String typePropsJson,
	                          @RequestParam(required = false,value = "brandIds[]") String[] brandIds,
	                          @RequestParam(required = false,value = "specIds[]") String[] specIds) {

        int row = goodsTypeService.addGoodsType(goodsType, typePropsJson, brandIds, specIds);

		return toAjax(row);
	}




	/**
	 * 修改商品类型
	 */
	@GetMapping("/edit/{typeId}")
	public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
	{
		GoodsType goodsType = goodsTypeService.selectGoodsTypeById(typeId);

		List<GoodsTypeProps> typePropsList = goodsTypePropsService.selectByTypeId(typeId);

		/** 设置扩展属性值 */
		if (CollectionUtils.isNotEmpty(typePropsList)) {
			for (GoodsTypeProps tp : typePropsList) {
				String propValues = tp.getTypePropsValueList().stream().map(bean -> bean.getName()).collect(Collectors.joining(","));
				tp.setTypePropValues(propValues);
			}
		}

		//查询关联的品牌
		List<Long> brandIdList = typeBrandService.selectBrandIdListByType(typeId);

		String brandIds = "";
		if (CollectionUtils.isNotEmpty(brandIdList)) {
			brandIds = StringUtils.join(brandIdList, ",");
		}

        //查询勾选关联的规格
        List<Long> specIdList = goodsTypeSpecService.selectSpecIdByTypeId(typeId);

        String specIds = "";
        if (CollectionUtils.isNotEmpty(specIdList)) {
            specIds = StringUtils.join(specIdList, ",");
        }

		mmap.put("goodsType", JsonUtil.objToStr(goodsType));
		mmap.put("typePropsList", JsonUtil.objToStr(typePropsList));
        mmap.put("specIds", specIds);
		mmap.put("brandIds", brandIds);

	    return prefix + "/edit";
	}



	@PostMapping("/getPropsAndSpecs")
	@ResponseBody
	public AjaxResult getPropsAndSpecs(Long typeId,Long goodsId) {

		Map<String,Object> data = new HashMap<>();

		List<GoodsTypeProps> typePropsList = goodsTypePropsService.selectByTypeId(typeId);

//		//查询勾选关联的规格
		List<Long> specIdList = goodsTypeSpecService.selectSpecIdByTypeId(typeId);
		Goods goods = (goodsId == null?new Goods():goodsService.selectGoodsById(goodsId));

		data.put("typePropsList",typePropsList);
		data.put("specIdList",specIdList);
		data.put("goods",goods);
		return AjaxResult.success(data);
	}
	
	/**
	 * 修改保存商品类型
	 */
	@RequiresPermissions("goods:goodsType:edit")
	@Log(title = "商品类型", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
    public AjaxResult editType(GoodsType goodsType, String typePropsJson,
	                           @RequestParam(required = false,value = "brandIds[]") String[] brandIds,
	                           @RequestParam(required = false,value = "specIds[]") String[] specIds) {
        int row = goodsTypeService.editGoodsType(goodsType, typePropsJson, brandIds, specIds);

		return toAjax(row);
	}
	
	/**
	 * 删除商品类型
	 */
	@RequiresPermissions("goods:goodsType:remove")
	@Log(title = "商品类型", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(goodsTypeService.deleteGoodsTypeByIds(ids));
	}
	
}
